package com.example.onlineeducation.ui.topic


import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.view.View
import com.example.onlineeducation.App
import com.example.onlineeducation.R
import com.example.onlineeducation.base.BaseActivity
import com.example.onlineeducation.data.constant.StudyConstants
import com.example.onlineeducation.data.entity.TopicEntity
import com.example.onlineeducation.databinding.TopicChildItemBinding
import com.example.onlineeducation.databinding.TopicGroupActivityBinding
import com.example.onlineeducation.utils.YcTimeUtils
import com.example.onlineeducation.viewmodel.TopicGroupVM
import com.yc.jetpacklib.extension.ycLoadImageNet
import com.yc.jetpacklib.extension.ycSetTextColorRes
import com.yc.jetpacklib.net.doSuccess
import com.yc.jetpacklib.recycleView.YcRecyclerViewAdapter
import com.yc.jetpacklib.refresh.YcRefreshSpecialUtil
import com.yc.jetpacklib.release.YcSpecialViewSmart

/*
* 专题分组列表
* */

@SuppressLint("SetTextI18n")
class TopicGroupListActivity : BaseActivity<TopicGroupActivityBinding>(TopicGroupActivityBinding::inflate) {

    private val mViewModel: TopicGroupVM by ycViewModels()
    private var mProId: String? = ""
    private var mGroupId: String? = ""
    private lateinit var mRefreshUtil: YcRefreshSpecialUtil

    companion object {
        private const val KEY_TOPIC_GROUP_ID = "key_topic_group_id"
        const val KEY_TOPIC_GROUP_NAME = "key_topic_group_name"
        fun newInstance(context: Context, groupId: String, groupName: String) {
            val intent = Intent(context, TopicGroupListActivity::class.java).apply {
                putExtra(KEY_TOPIC_GROUP_ID, groupId)
                putExtra(KEY_TOPIC_GROUP_NAME, groupName)
            }
            context.startActivity(intent)
        }
    }

    private val mTopicAdapter by YcRecyclerViewAdapter.ycLazyInitApply<TopicEntity, TopicChildItemBinding>(
        TopicChildItemBinding::inflate
    ) {
        mOnUpdate = {
            ivStudy.ycLoadImageNet(it.examImage, errorImg = R.drawable.ic_topic)
            tvStudyName.text = it.topicName
            tvTotalGrade.visibility = View.INVISIBLE
            tvStudyTime.text = "已学习" + YcTimeUtils.intSecondToMinStr(it.topicStudyTime.toInt())
            tvState.text = StudyConstants.showStudyResultText(it.topicPass)
            tvState.ycSetTextColorRes(StudyConstants.showStudyResultTextColor(it.topicPass))
            ivState.setImageResource(StudyConstants.showStudyResultIcon(it.topicPass))
        }
        mItemClick = {
            StudyContentActivity.newInstance(this@TopicGroupListActivity, it.topicId, it.needStudyTime, it.topicStudyTime)
        }
    }

    override fun TopicGroupActivityBinding.initView() {
        actionbarLayout.setTitle(intent.getStringExtra(KEY_TOPIC_GROUP_NAME)!!)
        mGroupId = intent.getStringExtra(KEY_TOPIC_GROUP_ID)!!
        rvList.apply {
            adapter = mTopicAdapter
            mRefreshUtil = YcRefreshSpecialUtil(this@TopicGroupListActivity, srlList, mTopicAdapter, YcSpecialViewSmart(rvList, flRefresh), false)
            srlList.setNoMoreData(true)
            mRefreshUtil.mRefreshAndLoadMore = { isRefresh: Boolean, pageIndex: Int, pageSize: Int ->
                if (isRefresh) {
                    getData()
                }
            }
        }
        mViewModel.mTopicListDataModel.observe {
            it.doSuccess { data ->
                mTopicAdapter.addAllData(data)
            }.ycShowNetError()
            mRefreshUtil.ycRefreshAndLoadMoreResult(it)
        }

        App.mInstant.mViewModel.mProInfoEntity.observe {
            if (it != null && mProId != it.proId) {
                mProId = it.proId
                getData()
            }
        }
    }

    private fun getData() {
        mViewModel.getTopicList(mProId!!, mGroupId!!)
    }

}
